<template>
  <el-dialog
    draggable
    overflow
    :title="titleMap[mode]"
    v-model="visible"
    :width="500"
    destroy-on-close
    @closed="$emit('closed')"
  >
    <el-container>
      <el-form
        :model="form"
        :rules="rules"
        ref="dialogForm"
        label-width="60px"
        label-position="right"
      >
        <el-row justify="space-around" :gutter="1" align="middle">
          <el-col :span="12">
            <el-form-item label="父库位" prop="parent_id">
              <skAutoChoose
                :name="'location'"
                v-model="data.parent.name"
                @select="locationchoose"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="编号" prop="code">
              <el-input label="编号" v-model="form.code" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="名称" prop="name">
              <el-input v-model="form.name" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="管理人" prop="admin_id">
              <skAutoChoose
                :name="'user'"
                v-model="data.admin.name"
                @select="adminchoose"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所属部门" prop="department_id">
              <skAutoChoose
                :name="'department'"
                v-model="data.department.name"
                @select="departmentchoose"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="地址" prop="address">
              <el-input v-model="form.address" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="外发仓？" prop="is_outsourcing">
              <el-switch
                v-model="form.is_outsourcing"
                :active-value="1"
                :inactive-value="0"
              ></el-switch>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="原料仓？" prop="is_material">
              <el-switch
                v-model="form.is_material"
                :active-value="1"
                :inactive-value="0"
              ></el-switch>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="部门默认" prop="default">
              <el-switch
                v-model="form.default"
                :active-value="1"
                :inactive-value="0"
              ></el-switch>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-container>
    <template #footer>
      <el-button @click="visible = false">取 消</el-button>
      <el-button type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
    </template>
  </el-dialog>
</template>

<script>
import skDicSelect from "@/components/skDicSelect";
import skUnitSelect from "@/components/skUnitSelect";
import skAutoChoose from "@/components/skAutoChoose";
import skSelectGroup from "@/components/skSelectGroup";
export default {
  emits: ["success", "closed"],
  components: { skSelectGroup, skDicSelect, skUnitSelect, skAutoChoose },
  data() {
    return {
      mode: "add",
      diccode: "CostType",
      unit: "All",
      titleMap: {
        add: "新增物料",
        edit: "编辑物料",
      },
      category: [],
      categoryBuf: [],
      form: {
        id: "",

        code: "",
        name: "",

        remark: "",
      },
      data: {
        parent: { name: "" },
        admin: { name: "" },
        department: { name: "" },
      },
      rules: {},
      visible: false,
      isSaveing: false,
    };
  },
  mounted() {
    this.getCateGory();
  },
  methods: {
    //显示
    open(mode = "add") {
      this.mode = mode;
      this.visible = true;
      return this;
    },
    locationchoose(data) {
      this.form.parent_id = data.id;
    },
    adminchoose(data) {
      this.form.admin_id = data.id;
    },
    departmentchoose(data) {
      this.form.department_id = data.id;
      this.form.address = data.name;
    },
    async getCateGory(query = "") {
      if (this.category == "") {
        this.category = await this.$API.basedata.material.category.list.get(query);
      }
      this.categoryBuf = this.category.data.filter((item) => {
        return item.name.toLowerCase().includes(query.toLowerCase());
      });
    },
    //表单提交方法
    submit() {
      this.$refs.dialogForm.validate(async (valid) => {
        if (valid) {
          this.isSaveing = true;
          var res = await this.$API.system.mpcLocaltion.save.post(this.form);
          this.isSaveing = false;
          if (res.code == 200) {
            this.$emit("success", this.form, this.mode);
            this.visible = false;
            this.$message.success("操作成功");
          } else {
            this.$alert(res.message, "提示", { type: "error" });
          }
        }
      });
    },
    //表单注入数据
    setData(data) {
      for (let i in data) {
        if (data[i] === null) delete data[i];
      }
      Object.assign(this.form, data);
      Object.assign(this.data, data);
      delete this.form.parent;
      delete this.form.updator;
      delete this.form.creator;
      delete this.form.deletor;
      delete this.form.department;
      delete this.form.admin;
      delete this.form.children;
      delete this.form._X_ROW_CHILD;

      // console.log(this.form);
    },
  },
};
</script>

<style></style>
